//准备actions对象——响应组件中用户的动作
//准备mutations对象——修改state中的数据
//准备state对象——保存具体的数据
//创建并暴露store
import router from '@/router'
import {createStore} from 'vuex'
import createPersistedState from 'vuex-persistedstate'

function addNewRoute(menuList){
	let routes = router.options.routes
	routes.forEach(routeItem=>{
		if(routeItem.path=="/Index"){
			menuList.forEach(menuItem=>{
				let childRoute={
						path:'/'+menuItem.menuclick,
						name:menuItem.menuname,
						meta:{
							title:menuItem.menuname
						},
						component:()=>import('../components/'+menuItem.menucomponent)
				}
				router.addRoute('index',childRoute)	
			})
		}
	})
}

const store = createStore({
    state:{
			menu:[]	
    },
    getters:{
		// getMenu(state, getters)
			// state 	全部的数据状态
			// getters  全部的计算属性
			getMenu(state){
				return state.menu
			}
    },
    mutations:{
		setMenu(state,menuList){
			state.menu = menuList
			addNewRoute(menuList)
		},
		setRouter(state,menuList){
			addNewRoute(menuList)
			
		}
    },
    actions:{
		async updateMenu({commit},menuList){
				commit('setMenu',menuList)
		}
    },
	plugins:[createPersistedState()]
})

export default store;
